package pers.mtx.hmw.filter;

import pers.mtx.hmw.cache.SingleTkMap;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AccesLogFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String tk = request.getHeader("tk");
        Object o = SingleTkMap.getInstance().getMap().getOrDefault(tk, null);
        if (o == null){
            response.sendError(401);
        }else {
            filterChain.doFilter(request,response);
        }

    }

    @Override
    public void destroy() {}
}